<?php
/*
 * 手机客户端用户-登录接口
 * 2014-06-03
 */
 

function checkRegKey($mobile,$imsi,$sourceacc,$key,$platform){
	if (isset($mobile) and isset($imsi) and isset($sourceacc) and isset($key)){
		//验证字段(手机号+IMSI+平台+来源+日期天 的MD5)
		$vkey = md5($mobile.$imsi.$platform.$sourceacc);
		if ($key==$vkey){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数

//常量
$mobile 	= $_REQUEST["mobile"];		
$userpwd 	= $_REQUEST["userpwd"];	
$key 		= $_REQUEST["key"];	
$imsi 		= $_REQUEST["imsi"];	
$platform = $_REQUEST["platform"];	
$sourceacc 	= $_REQUEST["sourceacc"];	

//登录
$rs=array("success"=>"0");
if (isset($mobile) and isset($userpwd)){
	if (isset($key)){
		$checkRegKey = checkRegKey($mobile,$imsi,$sourceacc,$key,$platform);
		if ($checkRegKey == true){
			//查找1天内登录失败次数
			$sql = "select counts from user_logerror where mobile = '".$mobile."' and date_add(dt, interval 1 day)>=NOW()";
			$db->query($sql);
			if($db->next_record()){
				$err_counts = $db->f("counts");
			}else{
				//删除登录失败记录
				$sql1 = "delete from user_logerror where mobile = '".$mobile."'";
				$db1->query($sql1);
			}
			//登录失败次数达到5次
			if ($err_counts==5){
				$rs=array("success"=>"0","errCode"=>"2","msg"=>"登录次数过多，请明天尝试！");
			}else{
				//密码是否正确
				$sql = "SELECT uid,uuid,userpwd,muuid FROM user_base WHERE mobile = '".$mobile."'";
				$db->query($sql);
				if($db->next_record()){
					$checkuspwd_uid = $db->f("uid");
					$checkuspwd_uuid = $db->f("uuid");
					$checkuspwd_userpwd = $db->f("userpwd");
					$checkuspwd_muuid = $db->f("muuid");
				}
				if (!$checkuspwd_uid){
					//"msg":"手机号未注册"
					$rs=array("success"=>"0","errCode"=>"1","msg"=>"手机号未注册！");
				}else{
					if ($checkuspwd_userpwd == $userpwd){
						$sql = "SELECT UUID() as uuid";
						$db->query($sql);
						if($db->next_record()){
							$newmuuid = $db->f("uuid");
						}
						$sql = "update user_base set muuid = '".$newmuuid."' where uid = '".$checkuspwd_uid."'";
						$db->query($sql);
						//插入登入日志
						$sql = "insert into user_login (uid,login";
						if (isset($platform) and strlen($platform)>0){
							$sql =$sql.",plat";
						}
						if (isset($imsi) and strlen($imsi)>0){
							$sql =$sql.",imsi";
						}
						$sql =$sql.",loginDT) values('".$checkuspwd_uid."','".$mobile."'";
						if (isset($platform) and strlen($platform)>0){
							$sql =$sql.",'".$platform."'";
						}
						if (isset($imsi) and strlen($imsi)>0){
							$sql =$sql.",'".$imsi."'";
						}
						$sql =$sql.",now())";
						$db->query($sql);
						//添加imsi信息
						$sql = "SELECT imsi from user_imsi where uid='".$checkuspwd_uid."'";
						$db->query($sql);
						if($db->next_record()){
							if ($db->f("imsi")!=$imsi){
								$sql1 = "update user_imsi set imsi='".$imsi."' where uid='".$checkuspwd_uid."'";
								$db1->query($sql1);
							}
						}else{
							$sql1 = "insert into user_imsi (uid,imsi) values ('".$checkuspwd_uid."','".$imsi."')";
							$db1->query($sql1);
						}
						//登录成功时删除登录失败记录
						if ($err_counts){
							$sql = "delete from user_logerror where mobile = '".$mobile."'";
							$db->query($sql);
						}
						$rs=array("success"=>"1","uuid"=>$newmuuid);
					}else{
						//登录失败时添加登录失败记录
						if ($err_counts){
							$err_counts = $err_counts + 1;
							$sql = "update user_logerror set counts='".$err_counts."' where mobile = '".$mobile."'";
							$db->query($sql);
						}else{
							$err_counts = 1;
							$sql = "insert into user_logerror (mobile,counts,dt) values ('".$mobile."','".$err_counts."',now())";
							$db->query($sql);
						}
						//登录失败次数达到5次
						if ($err_counts==5){
							$rs=array("success"=>"0","errCode"=>"2","msg"=>"登录次数过多，请明天尝试！");
						}else{
							$rs=array("success"=>"0","errCode"=>"1","msg"=>"还有".(5-$err_counts)."次输入机会，登录密码错误，请重新输入！");
						}
					}
				}
			}	
		}else{
			//"msg":"KEY不正确"
			$rs=array("success"=>"0","errCode"=>"3","msg"=>"登录异常！");
		}
	}else{
		//"msg":"缺少KEY"
		$rs=array("success"=>"0","errCode"=>"3","msg"=>"登录异常！");
	}
}



echo json_encode($rs);
?>
